# -*- coding: utf-8 -*-
import os
import time
import random
import PathConfig
import re

# 文件名替换 


fileNameList_old = []
fileNameList_new = []


randomKeyList = ['Tool','Event','Config','Name','Active','Hot','Pre','Add','Cen','Pos','Loo','Ren','Les','Old','Kan','Xiao','Da','Pro','Gong','Yu','Ming','Hai','Zei','Chi','Shen','Me','You','Too','Dom','Mai','Doc','She','New','Old','Dir','Set','Her','Mov','Vie','Con','Scr','For','Als','May','Find']



def replaceFileContent(file_dir):
	fs = os.listdir(file_dir)
	for dir in fs:
		if dir.startswith('.'):
			#过滤隐藏
			continue

		tmp_path = os.path.join(file_dir, dir)
		if not os.path.isdir(tmp_path):

			with open(tmp_path) as f:
				file_str = f.read()

			if not dir.endswith('.h') and not dir.endswith('.m') and not dir.endswith('.xib') and not dir.endswith('.pch'):
				continue
		
			for i in range(0,len(fileNameList_old)):

				oldName = fileNameList_old[i]
				newName = fileNameList_new[i]

				pattern = '\\b(%s)\\b' % oldName
				file_str = re.sub(pattern,newName,file_str)
				# print oldName + '  ' + newName

				if oldName.find('+') > 1 and newName.find('+') > 1:
					oldName = oldName.split('+')[1]
					newName = newName.split('+')[1]

					# print oldName + '  ' + newName

					pattern = '\\b(%s)\\b' % oldName
					file_str = re.sub(pattern,newName,file_str)

				# file_str = file_str.replace(oldName,newName)

			# print tmp_path
			print '替换文件内容  ' + tmp_path.split('/').pop()

			with open(tmp_path, 'w') as f:
				f.write(file_str)

		else:
			# 是文件夹,则递归调用
			replaceFileContent(tmp_path)


# 根据骆驼
def createRandomFileName(propertyName):

	# print propertyName
	# return propertyName

	propertyName_old = propertyName

	propertyName_old = propertyName_old.decode('utf-8')
	pattern = u'([A-Z]{1}[a-z0-9+]*)'
	results = re.findall(pattern,propertyName_old)

	listPart = []
	for s in results:
		listPart.append(s.encode('utf-8'))

	if len(listPart) > 1:
		
		name1 = listPart[len(listPart)-1]
		name2 = listPart[len(listPart)-2]

		name1_rep = name1
		name2_rep = name2

		# print 'name1 == %s  name2 == %s' % (name1,name2)

		randomList = randomKeyList
		# print 'randomList count == %s' % len(randomList) 
		if name2 in randomList:
			name2_rep = ''
			randomList.remove(name2)
			# print 'randomList count == %s' % len(randomList)

		name1_rep = (random.choice(randomList)).capitalize()+name1

		listPart[len(listPart)-1] = name1_rep
		listPart[len(listPart)-2] = name2_rep


	propertyName_new = ''.join(listPart)
	# print 'propertyName_new == ' + propertyName_new

	return propertyName_new


# 替换文件夹名字
def replaceFileName(filepath, filename, oldName, newName ):

	oldFile = os.path.join(filepath, filename)

	filename_new = filename.replace(oldName,newName,1)
	
	filenameAdd = filename.split('.')[0]
	fileType = filename.split('.')[1]
	
	fileHead = ''
	if filenameAdd.find('+') > 0:
		fileHead = filenameAdd.split('+')[0]
		filenameAdd = filenameAdd.split('+')[1]

		filename_new = filename_new.split('+')[1]



	if filenameAdd not in fileNameList_old:
		
		# print 'not in ' + filename_new
		print filename_new
		fileNameList_old.append(filenameAdd)
		
		filename_new = createRandomFileName(filename_new)
		fileNameList_new.append(filename_new)

		if len(fileHead) > 0:
			filename_new = fileHead + '+' + filename_new + '.' + fileType
		else:
			filename_new = filename_new + '.' + fileType

		# print 'filename_new == ' + filename_new
	else:
		# print 'in ' + filename_new

		index = fileNameList_old.index(filenameAdd)
		filename_new = fileNameList_new[index]

		if len(fileHead) > 0:
			filename_new = fileHead + '+' + filename_new + '.' + fileType
		else:
			filename_new = filename_new + '.' + fileType
		

		# print 'filename_new == ' + filename_new
		


	# print 'filename_new == ' + filename_new

	newFile = os.path.join(filepath, filename_new)   
	
	print '替换文件名  ' + filename_new

	inFile = open(oldFile)
	outFile = open(newFile, 'w')
	for line in inFile:
		outFile.write(line)
	inFile.close()
	outFile.close()
	os.remove(oldFile)
	

def traverse(file_dir):
    fs = os.listdir(file_dir)
    for dir in fs:
    	if dir.startswith('.'):
			#过滤隐藏
			continue

        tmp_path = os.path.join(file_dir, dir)
        if not os.path.isdir(tmp_path):
			if dir.startswith(PathConfig.file_pre_class_type_old):
				replaceFileName(file_dir, dir, PathConfig.file_pre_class_type_old, PathConfig.file_pre_class_type)

			# elif dir.find('+YH') > 0 :
			# 	replaceFileName(file_dir, dir, '+YH', '+'+PathConfig.file_pre_class_type)

			elif dir.find('+'+PathConfig.file_pre_class_type_old) > 0 :
				replaceFileName(file_dir, dir, '+'+PathConfig.file_pre_class_type_old, '+'+PathConfig.file_pre_class_type)
        else:
            # 是文件夹,则递归调用
            traverse(tmp_path)



def main():
	# print(PathConfig.rub_filepath)
	traverse(PathConfig.rub_filepath)

	replaceFileContent(PathConfig.rub_filepath)
	
	# print fileNameList_old
	# print fileNameList_new

	print('Finish done')

if __name__ == '__main__':
	main()

